﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.Core.Website;
using MvcEngine.Core;
using MvcEngine.Core.Extensions;
using MvcEngine.Mvc.Extensions;

namespace MvcEngine.Mvc.ModelBinders
{
    public class SiteModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            Site site = IoC.Resolve<Site>();
            site.Culture = request.Form.Get("Culture");
            site.DefaultPage = request.Form.Get("DefaultPage").ToGuid();
            site.LoginPage = request.Form.Get("LoginPage").ToNullableGuid();
            site.AccountPage = request.Form.Get("AccountPage").ToNullableGuid();
            site.Description = request.Form.Get("Description");
            site.FooterText = request.Form.Get("FooterText");
            site.HeaderText = request.Form.Get("HeaderText");
            site.Name = request.Form.Get("Name");
            site.RegistrationType = request.Form.Get("RegistrationType").ToInt();
            site.SkinId = request.Form.Get("Skin").ToGuid();

            site.Email = request.Form.Get("Email");
            site.SmtpUserName = request.Form.Get("SmtpUserName");
            site.SmtpPassword = request.Form.Get("SmtpPassword");
            site.SmtpServer = request.Form.Get("SmtpServer");
            site.SmtpServerPort = request.Form.Get("SmtpServerPort").ToInt();

            Validate(site, controllerContext);
            return site;
        }
    }
}
